﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using CarShop.Domain.Entities;
using CarShop.Security.Contracts;
using CarShop.UserInterface.Wpf.Core.Infrastructure;
using CarShop.UserInterface.Wpf.Infrastructure;
using CarShop.UserInterface.Wpf.ViewModels;
using CarShop.UserInterface.Wpf.Windows;

namespace CarShop.UserInterface.Wpf
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            CarShopPrincipal customPrincipal = new CarShopPrincipal(new CarShopIdentity(0, "", new string[0]));
            AppDomain.CurrentDomain.SetThreadPrincipal(customPrincipal);

            this.Initialize();

            this.ShowAuthenticationWindow();

         //   var membership = IoCContainer.Resolve<IMembershipProvider>();

      //      membership.CreateUser("user", "user@user.ru", "user");
      //      membership.CreateUser("admin", "admin@admin.ru", "admin");
      //      membership.CreateUser("adminuser", "adminuser@adminuser.ru", "adminuser");
            //membership.AddUserToRole(membership.FindUserByLoginOrEmail("user").Id, Role.NameFor.Customer);
            //membership.AddUserToRole(membership.FindUserByLoginOrEmail("admin").Id, Role.NameFor.Admin);
            //membership.AddUserToRole(membership.FindUserByLoginOrEmail("adminuser").Id, Role.NameFor.Customer);
            //membership.AddUserToRole(membership.FindUserByLoginOrEmail("adminuser").Id, Role.NameFor.Admin);

            //this.ShowMainWindow(new LoginCheckResults { RoleName = Role.Administrator });
        }

        private void ShowAuthenticationWindow()
        {
            var viewModel = IoCContainer.Resolve<AuthenticationWindowViewModel>();
            var window = new AuthenticationWindow { DataContext = viewModel };

            viewModel.LoggedIn +=
                (s, e) =>
                {
                    this.ShowMainWindow(e.LoginCheckResults);
                    window.Close();
                };
            window.Closing +=
                (s, e) =>
                {
                    viewModel.Dispose();
                };

            window.Show();
        }

        private void ShowMainWindow(LoginCheckResults loginCheckResults)
        {
            if (Thread.CurrentPrincipal.IsInRole(Role.NameFor.Admin) && Thread.CurrentPrincipal.IsInRole(Role.NameFor.Customer))
            {
                MainWindow window = new MainWindow();
                window.Show();
                return;
            }

            if (Thread.CurrentPrincipal.IsInRole(Role.NameFor.Admin))
            {
                AdministratorWindow window = new AdministratorWindow();
                window.Show();
                return;
            }

            if (Thread.CurrentPrincipal.IsInRole(Role.NameFor.Customer))
            {
                CustomerWindow window = new CustomerWindow();
                window.Show();
                return;
            }


            throw new ApplicationException(string.Format("Not supported role {0}", Thread.CurrentPrincipal.Identity.Name));
        }



        private void Initialize()
        {
            //FrameworkElement.LanguageProperty.OverrideMetadata(
            //    typeof(FrameworkElement),
            //    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
            IoCContainer.Build();
        }


    }
}
